package com.eimf.saf.security.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * @author eden
 * @date 2013-11-18 上午10:49:16
 * @Description: 验证码工具类
 */
public class ImageCodeUtil
{

    private static long seed = 0l;

    private static final int BACKGROUND_COLOR_MIN = 200;

    private static final int BACKGROUND_COLOR_MAX = 250;

    private static final int FOREGROUND_COLOR_MIN = 160;

    private static final int FOREGROUND_COLOR_MAX = 200;

    private static final int IMAGE_CODE_LENGTH = 4;

    private static int getRandom(final int min, final int max)
    {
        final Random random = new Random(ImageCodeUtil.seed);
        ImageCodeUtil.seed += random.nextLong();
        return min + random.nextInt(max - min + 1);
    }

    private static Color getRandColor(int min, int max)
    {
        if (min > ImageCodeUtil.BACKGROUND_COLOR_MAX)
        {
            min = ImageCodeUtil.BACKGROUND_COLOR_MAX;
        }
        if (max > ImageCodeUtil.BACKGROUND_COLOR_MAX)
        {
            max = ImageCodeUtil.BACKGROUND_COLOR_MAX;
        }
        return new Color(ImageCodeUtil.getRandom(min, max), ImageCodeUtil.getRandom(min, max), ImageCodeUtil.getRandom(
                min, max));
    }

    public static String generateImage(final int width, final int height, final int fontSize, final OutputStream out)
    {
        final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics g = image.getGraphics();
        // 设定背景色
        g.setColor(ImageCodeUtil.getRandColor(ImageCodeUtil.BACKGROUND_COLOR_MIN, ImageCodeUtil.BACKGROUND_COLOR_MAX));

        g.fillRect(0, 0, width, height);

        // 设定字体
        g.setFont(new Font("Times New Roman", Font.BOLD, 18));

        // 画边框
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, width - 1, height - 1);

        // 随机产生干扰线，使图象中的认证码不易被其它程序探测到
        g.setColor(ImageCodeUtil.getRandColor(ImageCodeUtil.FOREGROUND_COLOR_MIN, ImageCodeUtil.FOREGROUND_COLOR_MAX));
        // // 生成随机类

        final String s = RandomStringUtils.random(ImageCodeUtil.IMAGE_CODE_LENGTH, true, true);
        final Random random = new Random();
        for (int i = 0; i < 155; i++)
        {
            final int x2 = random.nextInt(width);
            final int y2 = random.nextInt(height);
            final int x3 = random.nextInt(12);
            final int y3 = random.nextInt(12);
            g.drawLine(x2, y2, x2 + x3, y2 + y3);
        }
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(s, 2, 16);
        g.dispose();
        final JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        try
        {
            encoder.encode(image);
            out.close();
        } catch (final Exception e)
        {
        }
        return s;
    }

    public static void generateAddCode(final HttpServletRequest request, final HttpServletResponse response)
            throws IOException
    {
        final String s = ImageCodeUtil.generateImage(60, 23, 18, response.getOutputStream());
        request.getSession().setAttribute(AppContant.VERIFICATION_CODE_KEY, s);
        response.getOutputStream().close();
    }

    public static boolean verifyAddCode(final String addcode, final HttpServletRequest request)
    {
        final String addcodeInSession = (String) request.getSession().getAttribute(AppContant.VERIFICATION_CODE_KEY);
        request.getSession().removeAttribute(AppContant.VERIFICATION_CODE_KEY);
        return (addcode != null) && (addcodeInSession != null) && (addcode.equals(addcodeInSession));
    }
}
